<template>
	<view class="content">
		<view class="person-block">
			<view class="user-photo-block">
				<button v-if="user" class="upload-photo-btn" open-type="chooseAvatar" @chooseavatar="changeAvatar"
					hover-class="none">
					<view class="tips">点击编辑</view>
					<image :src="user.avatar" class="user-photo ps"></image>
				</button>
				<image v-else src="@/static/images/mine/default_photo.jpg" class="user-photo"></image>
			</view>
			<view class="name-item" v-if="user">
				<view class="name">{{user.name}}</view>
				<!-- <view class="iconfont icon-fabu"
					@click="navigate('/page_other/personal/index?type=1&&name=编辑个人资料&&state='+user.id)">
					<text class="txt">编辑个人信息</text>
				</view> -->
			</view>
			<view class="name-item" v-else>
				<button open-type="getPhoneNumber" hover-class="none" @getphonenumber="getPhoneNumber"
					class="user-name">立即登录</button>
			</view>
		</view>
		<view class="info-block">
			<view class="info-list">
				<view class="info-item"
					@click="navigate('/page_other/personal/index?type=1&&name=编辑个人资料&&state='+user.id+'&auditStatus='+user.auditStatus)">
					<view class="left-item">
						<image src="../../static/images/mine/application.png" class="img1"></image>
						<view class="text">我的资料</view>
					</view>
					<view style="display: flex;align-items: center;">
						<view v-if="user.auditStatus==1" class="audit-status">未实名</view>
						<view v-if="user.auditStatus==2" class="audit-status1">已实名</view>
						<view v-if="user.auditStatus==3" class="audit-status2">审核中</view>
						<view v-if="user.auditStatus==4" class="audit-status">未通过</view>
						<view class="iconfont icon-gengduo"></view>
					</view>
				</view>
				<view class="info-item" @click="navigate('/page_other/apply/apply')">
					<view class="left-item">
						<image src="../../static/images/mine/application.png" class="img1"></image>
						<view class="text">我的报名</view>
					</view>
					<view class="iconfont icon-gengduo"></view>
				</view>
				<view class="info-item" @click="navigate('/page_other/my_integral/my_integral')">
					<view class="left-item">
						<image src="../../static/images/mine/integral.png" class="img"></image>
						<view class="text">我的积分</view>
					</view>
					<view class="iconfont icon-gengduo"></view>
				</view>
				<view class="info-item" @click="logout" v-if="user">
					<view class="left-item">
						<image src="../../static/images/mine/exit.png" class="img2"></image>
						<view class="text">退出登录</view>
					</view>
					<view class="iconfont icon-gengduo"></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getStorage,
		setStorage,
		removeStorage
	} from '@/util/auth.js';
	import {
		getInfoApi
	} from '@/api/mine.js'
	import {
		loginApi,
		updateAvatar
	} from '@/api/login.js'

	export default {
		data() {
			return {
				user: null
			}
		},
		methods: {
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			changeAvatar(e) {
				let loginInfo = getStorage('login');
				const filePath = e.detail.avatarUrl;
				uni.showLoading({
					title: '上传中',
					mask: true
				})
				uni.uploadFile({
					url: 'https://www.hxzxczx.cn/api/upload',
					name: 'file',
					filePath: filePath,
					header: {
						'authori-zation': loginInfo.token
					},
					success: (res) => {
						const data = JSON.parse(res.data);
						uni.hideLoading()
						if (data.code == 200) {
							updateAvatar({
								avatar: data.data
							}).then(res => {
								this.showToast('保存成功');
								this.user.avatar = res.avatar
								let oldUser = getStorage('login');
								oldUser.avatar = res.avatar
								setStorage('login', oldUser)
							}).catch(() => {
								uni.hideLoading()
							})
						} else {
							this.showToast(data.msg)
						}
					},
					fail: (res) => {
						uni.hideLoading()
						this.showToast('上传失败');
					}
				})
			},
			getPhoneNumber(e) {
				if (!e.detail.code) {
					this.showToast('用户取消了授权');
					return;
				}
				uni.showLoading({
					title: '提交中',
					mask: true,
				})
				const mobileCode = e.detail.code;
				wx.login({
					success: (login) => {
						const code = login.code;
						uni.showLoading({
							title: '加载中',
							mask: true
						})
						loginApi({
							code,
							mobileCode
						}).then((res) => {
							uni.hideLoading()
							const loginInfo = {
								token: res.token,
								...res.user
							};
							this.user = res.user
							setStorage('login', loginInfo, 2678400);
							setStorage('token', res.token, 2678400);
							this.showToast('登录成功');
						})
					}
				})
			},
			getDetail() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getInfoApi({}).then(res => {
					uni.hideLoading()
					this.user = res
					const loginInfo = {
						...res
					};
					setStorage('login', loginInfo, 2678400);
				}).catch(() => {
					uni.hideLoading()
				})
			},
			logout() {
				uni.showModal({
					title: '提示',
					content: `确定要退出重新登录吗？`,
					confirmColor: '#027BFD',
					success: (res) => {
						if (res.confirm) {
							removeStorage('login');
							removeStorage('token');
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}
					}
				})
			},
		},
		onShow() {
			const loginInfo = getStorage('login');
			if (loginInfo) {
				this.user = loginInfo
				this.getDetail()
			}
		},
	}
</script>

<style lang="scss">
	.content {
		width: 100%;

		.person-block {
			background: #157DFA;
			display: flex;
			//align-items: center;
			padding: 30rpx;
			height: 300rpx;

			.user-photo-block {
				width: 140rpx;
				height: 140rpx;
				border-radius: 50%;
				overflow: hidden;

				.upload-photo-btn {
					width: 100%;
					height: 100%;
					position: relative;
					padding: 0;

					.tips {
						font-size: 20rpx;
						width: 100%;
						position: absolute;
						bottom: 0;
						left: 0;
						color: #fff;
						display: flex;
						justify-content: center;
						align-items: center;
						background: rgba(0, 0, 0, 0.5);
						z-index: 1;
						height: 48rpx;
					}
				}

				.user-photo {
					width: 100%;
					height: 100%;
				}

				.user-photo.ps {
					position: absolute;
					left: 0;
					top: 0;

				}
			}

			.img {
				width: 150rpx;
				height: 150rpx;
				border-radius: 50%;
				background-color: #f5f5f5;
			}

			.name-item {
				display: flex;
				flex-direction: column;
				color: #fff;
				margin-left: 30rpx;
				margin-top: 50rpx;

				.name {
					margin-bottom: 20rpx;
					font-size: 38rpx;
				}

				.iconfont {
					font-size: 30rpx;

					.txt {
						margin-left: 15rpx;
						font-size: 28rpx;
					}
				}
			}

			.user-name {
				font-size: 36rpx;
				font-weight: bold;
				background: none;
				//margin-top: 20rpx;
				border-radius: 0;
				line-height: normal;
				padding: 0;
				color: #fff;

				&::after {
					border: none;
				}
			}
		}

		.info-block {
			display: flex;
			flex-direction: column;
			background: #F5F9FC;
			border-radius: 60rpx;
			margin-top: -80rpx;
			height: auto;
			position: relative;
			padding: 30rpx;
			min-height: 70vh;

			.info-list {
				background-color: #fff;
				border-radius: 20rpx;
				padding: 30rpx;

				.info-item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					line-height: 120rpx;
					border-bottom: 1px solid #eee;

					.left-item {
						display: flex;
						align-items: center;

						.img {
							width: 40rpx;
							height: 40rpx;
							margin-right: 12rpx;
						}

						.img1 {
							width: 36rpx;
							height: 36rpx;
							margin-right: 14rpx;
						}

						.img2 {
							width: 34rpx;
							height: 34rpx;
							margin-right: 15rpx;
							margin-left: 2rpx;
						}

						.iconfont {
							margin-right: 10rpx;
							color: #157DFA;
							font-weight: bold;
						}
					}

					.audit-status {
						background-color: #FF4500;
						color: #fff;
						height: 50rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						width: 120rpx;
						font-size: 26rpx;
						border-radius: 6rpx;
					}

					.audit-status1 {
						background-color: #1BA035;
						color: #fff;
						height: 50rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						width: 120rpx;
						font-size: 26rpx;
						border-radius: 6rpx;
					}

					.audit-status2 {
						background-color: #3D91F1;
						color: #fff;
						height: 50rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						width: 120rpx;
						font-size: 26rpx;
						border-radius: 6rpx;
					}
				}

				.info-item:last-child {
					border-bottom: none;
				}
			}
		}
	}
</style>